
Imports System.IO
Imports std
Imports storoomc
Imports storcore

Public Class slices
    Inherits storoomOper

    Private diskslices As diskslices = Nothing
    
    Private Function showsliceImpl(ByVal slice As slicebase) As String
        Return slice.filename()
    End Function

    Public Overrides Function showslice(ByVal slice As storageInterface) As String
        Return showsliceImpl(convertor.cast(Of slicebase)(slice))
    End Function

    Public Overrides Sub start()
        diskslices = New diskslices(configNaming.filter.value.slicestype.slices)
        openImpl(slices)
        MyBase.start()
    End Sub

    Protected Overrides Function capacityImpl(ByRef cap As UInt64, _
                                              ByVal slices As vector(Of storageInterface)) As Boolean
        Return diskslices.capacity(cap)
    End Function

    Protected Overrides Function openImpl(ByVal slices As vector(Of storageInterface)) As Boolean
        Dim filelist() As String = Nothing
        If diskslices.listfiles(filelist) Then
            Return diskslices.open(slices, filelist, False)
        Else
            Return False
        End If
    End Function

    Protected Overrides Function fullImpl(ByVal slices As vector(Of storageInterface)) As ternary
        Return diskslices.full(slices)
    End Function

    Private Function seekkey(ByVal key As String) As vector(Of storageInterface)
        Dim rtn As vector(Of storageInterface) = Nothing
        rtn = New vector(Of storageInterface)()
        Dim i As Int64
        i = 0
        While i < slices.size()
            If slices(i).seek(key) Then
                rtn.push_back(slices(i))
            End If
            i += 1
        End While
        Return rtn
    End Function

    Protected Overrides Function modifyImpl(ByVal key As String, ByVal buff As bytes, _
                                            ByRef slice As storageInterface, _
                                            ByVal slices As vector(Of storageInterface)) As Boolean
        If Not diskslices.sliceModifiable() Then
            Return MyBase.modifyImpl(key, buff, slice, slices)
        Else
            Return randomSelectModify(key, buff, slice, seekkey(key))
        End If
    End Function

    Protected Overrides Function writeImpl(ByVal key As String, ByVal buff As bytes, _
                                           ByRef slice As storageInterface, _
                                           ByVal slices As vector(Of storageInterface)) As Boolean
        'create more slices when the slices count is much lower than limit
        'as for each slice, it will provide a high performance when the data size contains is small

        'but found it will provide a low perf when open & start
        '                                           2010-06-11 14:09
        Dim i As Int32
        Dim j As Int32 = 0
        j = slices.size() * slices.size() \ diskslices.maxSliceCount()
        If j > (slices.size() >> 1) Then
            j = (slices.size() >> 1)
#If CANNOTCONFIRM Then
            ElseIf j < 1 AndAlso slices.size() >= threadCount() Then
                j = 1
#End If
        End If
        While j > 0
            i = rnd(0, slices.size(), True)
            slice = slices(i)
            If Not slice Is Nothing AndAlso Not slice.full() Then
                Exit While
            End If
            j -= 1
        End While

        If j = 0 OrElse slice Is Nothing Then
            assert(diskslices.create(slice), "cannot create an empty slice")
            assert(Not slice.full(), "a new slice is full, configuration issue")
            'here is a multi-writers & multi-readers model, but each reader will only read and not change,
            'so safe only lock writers
            SyncLock slices
                slices.push_back(slice)
            End SyncLock
        End If

        Return MyBase.writeImpl(key, buff, slice, slices)
    End Function
End Class
